home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Animation Routines / B - CellusoftAnimations.p < prev    next >
Text File  |  1993-03-31  |  10KB  |  244 lines

  1. {All procedures in this unit were created by Tony Small, author of Cellusoft games.  You may feel free to pass this code}
  2. {along to whom ever you like and look through the code and learn from it at your leisure.}
  3.  
  4. {If, however, you choose to use any of the code below in one of your programs, you are first obligated to pay the standard}
  5. {shareware fee of $15.  Upon payment of the fee, you may use any of the following procedures or the unit as a whole as many}
  6. {times as you choose in as many projects that you choose.   It would also be appreciated if you mention Tony Small in}
  7. {in the credits of any program you use his procedures in.}
  8.  
  9. {Those who pay the shareware fee of $15 will also be informed of future Cellusoft games and future Cellusoft programming}
  10. {routines.  Registered users will also be able to beta test all future Cellusoft games and be named in the credits when}
  11. {appropriate.}
  12.  
  13. {In order to register, please send $15 to:     Tony Small}
  14.                                                   {18606 Cassandra St.}
  15.                                                   {Tarzana, CA 91356}
  16.  
  17. {unit CellusoftAnimations© 1993 Tony Small - All Rights Reserved WorldWide}
  18.  
  19. {***Note*** - to use these routines, you don't necessarily have to understand them.  Simply read through the }
  20. {explanations in the interface section below and examine how they are used in the program sample file.}
  21.  
  22. {Questions?  Send mail or E-mail  AOL - TonyS33}
  23.  
  24. unit CellusoftAnimations;
  25.  
  26. interface
  27.  
  28.     uses
  29.         BasicRoutines;        {The BasicRoutines unit must be placed before this unit in order to use all the available routines.}
  30.  
  31.     const
  32.         MaxAnimations = 15;    {This number specifies the maximum number of sprites you plan to use in your program.}
  33.                                 {The higher the number, the more memory your program will require.}
  34.  
  35.     type
  36.         AnimationRecord = record        {This record is used by the animationr routines.}
  37.                 active: boolean;
  38.                 lRect, cRect, pRect: rect;
  39.                 theGraf, theMaskGraf: GrafPtr;
  40.             end;
  41.  
  42.     var
  43.         animArray: array[1..MaxAnimations] of AnimationRecord;
  44.         tr: array[1..4] of rect;    {An array of four temporary rects (tr) used periodically throughout the unit.}
  45.         Pic_Handle: PicHandle;
  46.         theregion: RgnHandle;
  47.  
  48. {The following two procedures will alow you, in one line of code, to create a port of any size, and place a picture}
  49. {from a PICT resource onto the port.  If you want the port to be blank, specify Picture as -1.}
  50. {ThePort - The name of the port variable, of type GrafPtr.}
  51. {Picture - The ID of the PICT resource to place on the port.  Specify as -1 if you want the port to be blank.}
  52. {right, bottom - integers specifying the size of the port.}
  53.     procedure DoColorPort (var ThePort: GrafPtr; Picture, right, bottom: integer);
  54.     procedure DoPort (var ThePort: GrafPtr; Picture, right, bottom: integer);
  55.  
  56. {Call these procedures to release the memory used up by color and black and white ports.}
  57.     procedure Trash (var ThePort: GrafPtr);
  58.     procedure TrashColor (var ThePort: GrafPtr);
  59.  
  60. {This procedure will take the GrafPtr Origin, and create a port of identical size called Destination and effectively perform}
  61. {a horizontal swap of the picture on Origin.  This procedure is good to use if you want to save disk space by not having}
  62. {to put horizontally swapped pictures into the resource.  Set the variable Color to true if the GrafPtr is a color port.}
  63.     procedure HorizontalFlip (var Origin, Destination: GrafPtr; Color: boolean);
  64.  
  65. {The following two procedures are simply easier ways to call sometimes complicated system routines.}
  66. {Copy replaces CopyBits.  With this, you do not need to put in ^.portbits and specify the regionhandle and other odd variables.}
  67. {This simply copies from Port1 to Port2, from Rect1 to Rect2.}
  68. {MoveRect moves TheRect, the specified integers.  MoveRect is used by other CellusoftAnimations routines.}
  69.     procedure Copy (var Port1, Port2: GrafPtr; var Rect1, Rect2: rect);
  70.     procedure MoveRect (var TheRect: rect; left, up, right, down: integer);     {Mainly used in the Animation procedure, this is a quick way to move a rect.}
  71.  
  72. {The following procedures help to manage the actual animation process.  Call InitAnimations after every animation loop.}
  73. {InitAnimations will reset the animArray to its original state.}
  74. {Call SetAnimation to add an animation to the array. tlRect specifies where the animated graphic was last on the screne.}
  75. {tcRect specifies where the animated graphic is now on the screne.  tpRect specifies the location of the graphic on the GrafPtr}
  76. {where the actual picture to be animated is located.  ttheGraf is the port that holds that picture and tthemaskGraf is the port}
  77. {with the mask of that picture.}
  78. {AnimateArray will animate all of the animations you specified with each SetAnimation call.  The maximum number of animations there}
  79. {can be at the same time is only limited by the constant MaxAnimations.  This procedure also handled the collisions of any}
  80. {number of animations without flicker.}
  81. {pureback is the port that holds the original picture of the background graphic.  offLoad is a blank port the same size of the}
  82. {MainWindow.  MainWindow is the pointer to the window where the user will see the animations take place.}
  83. {***Note*** - The method of animation I used was developed by John Calhoun, author of Glider 4.0.  If you would like to learn}
  84. {move about the flicker free technique used by his programs and by these routines, then look for either Glypha 2.0 source code}
  85. {or Glypha Primer offered by John Calhoun available on AOL and other bulletin boards.}
  86.     procedure InitAnimations;
  87.     procedure AnimateArray (pureback, offLoad: GrafPtr; MainWindow: WindowPtr);
  88.     procedure SetAnimation (tlRect, tcRect, tpRect: rect; ttheGraf, tthemaskGraf: GrafPtr);
  89.  
  90. implementation
  91.  
  92.     procedure AnimateArray (pureback, offLoad: GrafPtr; MainWindow: WindowPtr);
  93.         var
  94.             i, j: integer;
  95.             wRect: array[1..MaxAnimations] of rect;
  96.             col: array[1..MaxAnimations] of boolean;
  97.     begin
  98.         for i := 1 to MaxAnimations do
  99.             with animArray[i] do
  100.                 if active then
  101.                     unionRect(lRect, cRect, wRect[i]);
  102.         for i := 1 to MaxAnimations do
  103.             with animArray[i] do
  104.                 if active then
  105.                     begin
  106.                         for j := i + 1 to MaxAnimations do
  107.                             col[j] := false;
  108.                         tr[2] := wRect[i];
  109.                         for j := i + 1 to MaxAnimations do
  110.                             with animArray[j] do
  111.                                 if active then
  112.                                     if sectRect(tr[2], wRect[j], tr[1]) then
  113.                                         begin
  114.                                             col[j] := true;
  115.                                             animArray[j].active := false;
  116.                                             unionRect(tr[2], wRect[j], tr[2]);
  117.                                         end;
  118.                         col[i] := true;
  119.                         begin
  120.                             copy(pureback, offLoad, tr[2], tr[2]);
  121.                             for j := i to MaxAnimations do
  122.                                 if col[j] then
  123.                                     with AnimArray[j] do
  124.                                         begin
  125.                                             copymask(theGraf^.portbits, theMaskGraf^.portbits, offLoad^.portbits, pRect, pRect, cRect);
  126.                                         end;
  127.                             copy(offLoad, MainWindow, tr[2], tr[2]);
  128.                         end;
  129.                     end;
  130.     end;
  131.  
  132.     procedure SetAnimation (tlRect, tcRect, tpRect: rect; ttheGraf, tthemaskGraf: GrafPtr);
  133.         var
  134.             i: integer;
  135.     begin
  136.         i := 1;
  137.         while animArray[i].active and (i < MaxAnimations) do
  138.             i := i + 1;
  139.         with animArray[i] do
  140.             begin
  141.                 active := true;
  142.                 lRect := tlRect;
  143.                 cRect := tcRect;
  144.                 pRect := tpRect;
  145.                 theGraf := ttheGraf;
  146.                 theMaskGraf := ttheMaskGraf;
  147.             end;
  148.     end;
  149.  
  150.     procedure InitAnimations;
  151.         var
  152.             i: integer;
  153.     begin
  154.         for i := 1 to MaxAnimations do
  155.             animArray[i].active := false;
  156.     end;
  157.  
  158.     procedure Copy (var Port1, Port2: GrafPtr; var Rect1, Rect2: rect);
  159.  
  160.     begin
  161.         copyBits(Port1^.portbits, Port2^.portbits, Rect1, Rect2, 0, theregion);
  162.     end;
  163.  
  164.     procedure MoveRect (var TheRect: rect; left, up, right, down: integer);     {Mainly used in the Animation procedure, this is a quick way to move a rect.}
  165.     begin
  166.         TheRect.left := TheRect.left + left;
  167.         TheRect.top := TheRect.top + up;
  168.         TheRect.right := TheRect.right + right;
  169.         TheRect.bottom := TheRect.bottom + down;
  170.     end;
  171.  
  172.     procedure HorizontalFlip (var Origin, Destination: GrafPtr; Color: boolean);
  173.         var
  174.             i: integer;
  175.     begin
  176.         tr[1] := Origin^.portrect;
  177.         if Color then
  178.             DoColorPort(Destination, -1, tr[1].right, tr[1].bottom)
  179.         else
  180.             DoPort(Destination, -1, tr[1].right, tr[1].bottom);
  181.         setRect(tr[2], 0, 0, 1, tr[1].bottom);
  182.         setRect(tr[3], tr[1].right - 1, 0, tr[1].right, tr[1].bottom);
  183.         for i := 1 to tr[1].right do
  184.             begin
  185.                 copy(Origin, Destination, tr[2], tr[3]);
  186.                 moveRect(tr[2], 1, 0, 1, 0);
  187.                 moveRect(tr[3], -1, 0, -1, 0);
  188.             end;
  189.     end;
  190.  
  191.     procedure Trash (var ThePort: GrafPtr);
  192.     begin
  193.         DisposeOffScreenPort(ThePort);
  194.     end;
  195.  
  196.     procedure TrashColor (var ThePort: GrafPtr);
  197.     begin
  198.         DisposeOffScreenColorPort(CGrafPtr(ThePort));
  199.     end;
  200.  
  201.     procedure DoColorPort (var ThePort: GrafPtr; Picture, right, bottom: integer);
  202.  
  203.     {This procedure creates a PixMap within ThePort, with PICT resource Picture, and with rect size right and bottom.}
  204.  
  205.     begin
  206.         SetRect(tR[1], 0, 0, right, bottom);        {Set the rect temprect to the size designated by the caller.}
  207.  
  208.         ThePort := GrafPtr(NewOffScreenColorPort(tR[1]));    {Creates the PixMap.}
  209.         if thePort <> nil then                                    {If there wasnt a memory problem, then...}
  210.             begin
  211.                 SetPort(ThePort);                        {Set the port to the port its creating.}
  212.  
  213.                 EraseRect(tR[1]);                                {Set the port to white, to rid of the garbage.}
  214.                 if Picture <> -1 then
  215.                     begin
  216.                         Pic_Handle := GetPicture(Picture);                {Get the picture from the resource.}
  217.                         DrawPicture(Pic_Handle, tR[1]);                {Draw it onto the port.}
  218.                     end;
  219.             end;
  220.     end;
  221.  
  222.     procedure DoPort (var ThePort: GrafPtr; Picture, right, bottom: integer);
  223.  
  224.     {This procedure creates a PixMap within ThePort, with PICT resource Picture, and with rect size right and bottom.}
  225.  
  226.     begin
  227.         SetRect(tR[1], 0, 0, right, bottom);        {Set the rect temprect to the size designated by the caller.}
  228.  
  229.         ThePort := NewOffScreenPort(tR[1]);    {Creates the PixMap.}
  230.         if thePort <> nil then                                    {If there wasnt a memory problem, then...}
  231.             begin
  232.                 SetPort(ThePort);                        {Set the port to the port its creating.}
  233.                 EraseRect(tR[1]);                                {Set the port to white, to rid of the garbage.}
  234.                 if Picture <> -1 then
  235.                     begin
  236.                         Pic_Handle := GetPicture(Picture);                {Get the picture from the resource.}
  237.                         DrawPicture(Pic_Handle, tR[1]);                {Draw it onto the port.}
  238.                     end;
  239.             end;
  240.     end;
  241.  
  242.  
  243.  
  244. end.